package com.roy.sms.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.roy.sms.model.Column;
import com.roy.sms.service.ColumnService;
import com.roy.sms.vo.ColumnVO;
import com.roy.sms.vo.TreeNodeVO;

@Controller
public class ColumnController extends BaseController {

	@Resource
	private ColumnService columnService;

	@RequestMapping("/column/manager.do")
	public String manager() {
		return "column/columnManager";
	}

	@RequestMapping("/column/list.do")
	@ResponseBody
	public List<ColumnVO> list(int pid) {
		return columnService.list(pid);
	}

	@RequestMapping("/column/initEdit.do")
	public String initEdit(Integer id, ModelMap modelMap, Integer pid) {
		ColumnVO columnVO = null;
		if (id != null) {
			columnVO = columnService.load(id);
		} else {
			columnVO = new ColumnVO();
			if(pid == -1){
				columnVO.setType(Column.TYPE_FRANT);
			} else if(pid == -2){
				columnVO.setType(Column.TYPE_BACK);
			} else {
				ColumnVO p = columnService.load(pid);
				columnVO.setType(p.getType());
			}
			columnVO.setPid(pid);
		}
		modelMap.put("columnVO", columnVO);
		return "column/editColumn";
	}

	@RequestMapping("/column/saveColumn.do")
	@ResponseBody
	public String saveUser(ColumnVO columnVO) {
		if (columnVO.getId() == 0) {
			columnService.add(columnVO);
		} else {
			columnService.update(columnVO);
		}
		return SUCCESS;
	}

	@RequestMapping("/column/deleteColumns.do")
	@ResponseBody
	public String deleteUsers(String ids) {
		columnService.delete(ids);
		return SUCCESS;
	}
	
	@RequestMapping("/column/columnTree.do")
	@ResponseBody
	public List<TreeNodeVO> getColumnTree(int type){
		return columnService.findColumnTree(type);
	}

	@RequestMapping("/column/articleColumnTree.do")
	@ResponseBody
	public List<TreeNodeVO> getFrantTree(){
		return columnService.getArticleColumnTree();
	}
	
	
}
